package com.skt.common_hr.overtime.model
{
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.overtime.model.vo.MonthlyDeptVO;
	import com.skt.common_hr.overtime.model.vo.MonthlyMemberVO;
	import com.wcl.utils.DateUtil;
	import com.wcl.utils.StringUtil;
	
	import mx.collections.ArrayCollection;
	import mx.formatters.DateFormatter;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class MonthlyMemberListProxy extends CommonProxy
	{
		public static const NAME:String = "MonthlyMemberListProxy";
		public static const LOAD_MONTHLY_MEMBER_LIST_COMPLETE:String = "loadMonthlyMemListComplete";
		public static const LOAD_MONTHLY_MEMBER_LIST_FAILED:String = "loadMonthlyMemListFailed";

		public var year:int;
		public var month:int;
		
		public var list:ArrayCollection;
		public var ORGEH:String;
		public var ORGTX:String;
		public var bReadOnly:Boolean = false;
		
		public var deptList:ArrayCollection;
		
		public function MonthlyMemberListProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		//병원목록
		public function load(month:String, dept:String, bSub:Boolean=false):void
		{
			
			var params:Object = new Object();
			if(month=="") {
				var df:DateFormatter = new DateFormatter();
				df.formatString = "YYYYMM";
				month = df.format(new Date());
			}
			params.I_MONTHS = month;
			params.I_ORGEH = dept;
			params.I_DIS = (bSub) ? "X" : "";
			// 현재 조회 기준 연워을 저장함..
			setYearMonth(params.I_MONTHS);
			
			loadData("Z_HR_0116", params, listResult, listFault);
		}
		
		
		private function  setYearMonth(str:String):void
		{
			year = parseInt(str.substr(0,4));
			month = parseInt(str.substr(4));
		}
		public function getOT(d:Date, dept:String):Number
		{
			for(var i:int=0; i<list.length; i++) {
				//list.getItemAt(i)["DAY"
			}
			
			return 0;
		}
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		private function listResult(event:ResultEvent):void
		{
			if(retCode==0 || retCode==1) {  
				list = parseList(event.result.T_OUT);
				list = parseVacation(list, event.result.T_OUT2);
				//list = parseList(list, event.target.T_OUT3);
				ORGEH = event.result.E_ORGEH;
				ORGTX = event.result.E_ORGTX;
				bReadOnly = event.result.E_READONLY=="X";
				facade.sendNotification(LOAD_MONTHLY_MEMBER_LIST_COMPLETE, this);
			}
			else {
				facade.sendNotification(LOAD_MONTHLY_MEMBER_LIST_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function listFault(event:FaultEvent):void
		{
			facade.sendNotification(LOAD_MONTHLY_MEMBER_LIST_FAILED, this);
		}
	



		private function parseList(src:ArrayCollection):ArrayCollection
		{
			var list:ArrayCollection = new ArrayCollection;
			for each(var rec:Object in src) {
				var vo:MonthlyMemberVO = new MonthlyMemberVO;
				vo.PERNR = StringUtil.trimFront(rec.PERNR, "0");
				vo.ENAME = rec.ENAME;
				vo.JIKCHAK = rec.JIKCHAK;
				vo.ORGEH = rec.ORGEH;
				vo.ORGTX = rec.ORGTX;
				vo.sum = 0;
				vo.DAY01 = (rec.DAY01==0) ? "" : Number(rec.DAY01).toString();	vo.sum += Number(rec.DAY01);
				vo.DAY02 = (rec.DAY02==0) ? "" : Number(rec.DAY02).toString();	vo.sum += Number(rec.DAY02);
				vo.DAY03 = (rec.DAY03==0) ? "" : Number(rec.DAY03).toString();	vo.sum += Number(rec.DAY03);
				vo.DAY04 = (rec.DAY04==0) ? "" : Number(rec.DAY04).toString();	vo.sum += Number(rec.DAY04);
				vo.DAY05 = (rec.DAY05==0) ? "" : Number(rec.DAY05).toString();	vo.sum += Number(rec.DAY05);
				vo.DAY06 = (rec.DAY06==0) ? "" : Number(rec.DAY06).toString();	vo.sum += Number(rec.DAY06);
				vo.DAY07 = (rec.DAY07==0) ? "" : Number(rec.DAY07).toString();	vo.sum += Number(rec.DAY07);
				vo.DAY08 = (rec.DAY08==0) ? "" : Number(rec.DAY08).toString();	vo.sum += Number(rec.DAY08);
				vo.DAY09 = (rec.DAY09==0) ? "" : Number(rec.DAY09).toString();	vo.sum += Number(rec.DAY09);
				vo.DAY10 = (rec.DAY10==0) ? "" : Number(rec.DAY10).toString();	vo.sum += Number(rec.DAY10);
				vo.DAY11 = (rec.DAY11==0) ? "" : Number(rec.DAY11).toString();	vo.sum += Number(rec.DAY11);
				vo.DAY12 = (rec.DAY12==0) ? "" : Number(rec.DAY12).toString();	vo.sum += Number(rec.DAY12);
				vo.DAY13 = (rec.DAY13==0) ? "" : Number(rec.DAY13).toString();	vo.sum += Number(rec.DAY13);
				vo.DAY14 = (rec.DAY14==0) ? "" : Number(rec.DAY14).toString();	vo.sum += Number(rec.DAY14);
				vo.DAY15 = (rec.DAY15==0) ? "" : Number(rec.DAY15).toString();	vo.sum += Number(rec.DAY15);
				vo.DAY16 = (rec.DAY16==0) ? "" : Number(rec.DAY16).toString();	vo.sum += Number(rec.DAY16);
				vo.DAY17 = (rec.DAY17==0) ? "" : Number(rec.DAY17).toString();	vo.sum += Number(rec.DAY17);
				vo.DAY18 = (rec.DAY18==0) ? "" : Number(rec.DAY18).toString();	vo.sum += Number(rec.DAY18);
				vo.DAY19 = (rec.DAY19==0) ? "" : Number(rec.DAY19).toString();	vo.sum += Number(rec.DAY19);
				vo.DAY20 = (rec.DAY20==0) ? "" : Number(rec.DAY20).toString();	vo.sum += Number(rec.DAY20);
				vo.DAY21 = (rec.DAY21==0) ? "" : Number(rec.DAY21).toString();	vo.sum += Number(rec.DAY21);
				vo.DAY22 = (rec.DAY22==0) ? "" : Number(rec.DAY22).toString();	vo.sum += Number(rec.DAY22);
				vo.DAY23 = (rec.DAY23==0) ? "" : Number(rec.DAY23).toString();	vo.sum += Number(rec.DAY23);
				vo.DAY24 = (rec.DAY24==0) ? "" : Number(rec.DAY24).toString();	vo.sum += Number(rec.DAY24);
				vo.DAY25 = (rec.DAY25==0) ? "" : Number(rec.DAY25).toString();	vo.sum += Number(rec.DAY25);
				vo.DAY26 = (rec.DAY26==0) ? "" : Number(rec.DAY26).toString();	vo.sum += Number(rec.DAY26);
				vo.DAY27 = (rec.DAY27==0) ? "" : Number(rec.DAY27).toString();	vo.sum += Number(rec.DAY27);
				vo.DAY28 = (rec.DAY28==0) ? "" : Number(rec.DAY28).toString();	vo.sum += Number(rec.DAY28);
				vo.DAY29 = (rec.DAY29==0) ? "" : Number(rec.DAY29).toString();	vo.sum += Number(rec.DAY29);
				vo.DAY30 = (rec.DAY30==0) ? "" : Number(rec.DAY30).toString();	vo.sum += Number(rec.DAY30);
				vo.DAY31 = (rec.DAY31==0) ? "" : Number(rec.DAY31).toString();	vo.sum += Number(rec.DAY31);
				list.addItem(vo);
			}
			return list;
		}	
		
		private function parseVacation(src:ArrayCollection, vacList:ArrayCollection):ArrayCollection
		{
			for each(var vac:Object in vacList) {
				if(vac.DGBN.toString() == "1" && (vac.WSTATUS.toString()=="2" || vac.WSTATUS.toString()=="3")) {	// 휴가 인경우
					setVaction(src, StringUtil.trimFront(vac.PERNR, "0"), vac.AWATX, vac.WSTATUS, DateUtil.stringToDateTime(vac.BEGDA), DateUtil.stringToDateTime(vac.ENDDA));
				}
				else if((vac.AWART.toString() == "0401" || vac.AWART.toString() == "0402") && (vac.WSTATUS.toString()=="2" || vac.WSTATUS.toString()=="3")) {	// 출장인경우
					setBizTrip(src, StringUtil.trimFront(vac.PERNR, "0"), vac.AWATX, vac.WSTATUS, DateUtil.stringToDateTime(vac.BEGDA), DateUtil.stringToDateTime(vac.ENDDA));
				}
			}
			return src;
		}
		
		private function setBizTrip(src:ArrayCollection, pernr:String, type:String, status:String, beg:Date, end:Date):void
		{
			beg.hours = beg.minutes = beg.seconds = beg.milliseconds = 0;
			end.hours = end.minutes = end.seconds = end.milliseconds = 0;
			if(Number(beg) <= Number(end)) {
				var tmp:Date = new Date(beg);
				do {
					var rec:MonthlyMemberVO = getMember(src, pernr);
					if(rec!=null && inMonth(tmp)) {
						if(Number(rec[getColumnName(tmp)])==0) {
							rec[getColumnName(tmp)] = "출장"; //type.substr(0,2);
							if(status=="2") {
								//rec[getTipColumnName(tmp)] = "결재중";
								rec[getColumnName(tmp)] += ":2";
							}
							else if(status=="3") {
								rec[getColumnName(tmp)] += ":3";
								//rec[getTipColumnName(tmp)] = "결재완료";
							}
						}
						else {
							rec[getColumnName(tmp)] = "(ot)";
						}
					}
					tmp.date += 1;
				} while(Number(tmp)<=Number(end));
			} 
		}

		private function setVaction(src:ArrayCollection, pernr:String, type:String, status:String, beg:Date, end:Date):void
		{
			beg.hours = beg.minutes = beg.seconds = beg.milliseconds = 0;
			end.hours = end.minutes = end.seconds = end.milliseconds = 0;
			if(Number(beg) <= Number(end)) {
				var tmp:Date = new Date(beg);
				do {
					var rec:MonthlyMemberVO = getMember(src, pernr);
					if(rec!=null && inMonth(tmp)) {
						if(Number(rec[getColumnName(tmp)])==0) {
							rec[getColumnName(tmp)] = type.substr(0,2);
							if(status=="2") {
								//rec[getTipColumnName(tmp)] = "결재중";
								rec[getColumnName(tmp)] += ":2";
							}
							else if(status=="3") {
								rec[getColumnName(tmp)] += ":3";
								//rec[getTipColumnName(tmp)] = "결재완료";
							}
						}
						else {
							rec[getColumnName(tmp)] = "(ot)";
						}
					}
					tmp.date += 1;
				} while(Number(tmp)<=Number(end));
			} 
		}
		
		private function getMember(src:ArrayCollection, pernr:String):MonthlyMemberVO
		{
			for each(var vo:MonthlyMemberVO in src) {
				if(vo.PERNR == pernr) return vo;
			}
			return null;
		}
		
		private function inMonth(tmp:Date):Boolean
		{
			if(tmp.fullYear == year && tmp.month == (month-1)) return true;
			return false;
		}
		 
		private function getColumnName(d:Date):String
		{
			var day:String = "DAY"
			day += ((d.date)<10) ? "0" + d.date.toString() : d.date.toString();
			return day;
		}
		
		private function getTipColumnName(d:Date):String
		{
			var day:String = "tip"
			day += ((d.date)<10) ? "0" + d.date.toString() : d.date.toString();
			return day;
		}

		private function parseDept(src:ArrayCollection):ArrayCollection
		{
			var list:ArrayCollection = new ArrayCollection;
			for each(var rec:Object in src) {
				var vo:MonthlyDeptVO = new MonthlyDeptVO;
				vo.ORGEH = rec.ORGEH;
				vo.ORGTX = rec.ORGTX;
				vo.ZYEAR = rec.ZYEAR;
				vo.ZMONTH = rec.ZMONTH;
				vo.sum = 0;
				vo.DAY01 = (rec.DAY01==0) ? "" : Number(rec.DAY01).toString();	vo.sum += Number(rec.DAY01);
				vo.DAY02 = (rec.DAY02==0) ? "" : Number(rec.DAY02).toString();	vo.sum += Number(rec.DAY02);
				vo.DAY03 = (rec.DAY03==0) ? "" : Number(rec.DAY03).toString();	vo.sum += Number(rec.DAY03);
				vo.DAY04 = (rec.DAY04==0) ? "" : Number(rec.DAY04).toString();	vo.sum += Number(rec.DAY04);
				vo.DAY05 = (rec.DAY05==0) ? "" : Number(rec.DAY05).toString();	vo.sum += Number(rec.DAY05);
				vo.DAY06 = (rec.DAY06==0) ? "" : Number(rec.DAY06).toString();	vo.sum += Number(rec.DAY06);
				vo.DAY07 = (rec.DAY07==0) ? "" : Number(rec.DAY07).toString();	vo.sum += Number(rec.DAY07);
				vo.DAY08 = (rec.DAY08==0) ? "" : Number(rec.DAY08).toString();	vo.sum += Number(rec.DAY08);
				vo.DAY09 = (rec.DAY09==0) ? "" : Number(rec.DAY09).toString();	vo.sum += Number(rec.DAY09);
				vo.DAY10 = (rec.DAY10==0) ? "" : Number(rec.DAY10).toString();	vo.sum += Number(rec.DAY10);
				vo.DAY11 = (rec.DAY11==0) ? "" : Number(rec.DAY11).toString();	vo.sum += Number(rec.DAY11);
				vo.DAY12 = (rec.DAY12==0) ? "" : Number(rec.DAY12).toString();	vo.sum += Number(rec.DAY12);
				vo.DAY13 = (rec.DAY13==0) ? "" : Number(rec.DAY13).toString();	vo.sum += Number(rec.DAY13);
				vo.DAY14 = (rec.DAY14==0) ? "" : Number(rec.DAY14).toString();	vo.sum += Number(rec.DAY14);
				vo.DAY15 = (rec.DAY15==0) ? "" : Number(rec.DAY15).toString();	vo.sum += Number(rec.DAY15);
				vo.DAY16 = (rec.DAY16==0) ? "" : Number(rec.DAY16).toString();	vo.sum += Number(rec.DAY16);
				vo.DAY17 = (rec.DAY17==0) ? "" : Number(rec.DAY17).toString();	vo.sum += Number(rec.DAY17);
				vo.DAY18 = (rec.DAY18==0) ? "" : Number(rec.DAY18).toString();	vo.sum += Number(rec.DAY18);
				vo.DAY19 = (rec.DAY19==0) ? "" : Number(rec.DAY19).toString();	vo.sum += Number(rec.DAY19);
				vo.DAY20 = (rec.DAY20==0) ? "" : Number(rec.DAY20).toString();	vo.sum += Number(rec.DAY20);
				vo.DAY21 = (rec.DAY21==0) ? "" : Number(rec.DAY21).toString();	vo.sum += Number(rec.DAY21);
				vo.DAY22 = (rec.DAY22==0) ? "" : Number(rec.DAY22).toString();	vo.sum += Number(rec.DAY22);
				vo.DAY23 = (rec.DAY23==0) ? "" : Number(rec.DAY23).toString();	vo.sum += Number(rec.DAY23);
				vo.DAY24 = (rec.DAY24==0) ? "" : Number(rec.DAY24).toString();	vo.sum += Number(rec.DAY24);
				vo.DAY25 = (rec.DAY25==0) ? "" : Number(rec.DAY25).toString();	vo.sum += Number(rec.DAY25);
				vo.DAY26 = (rec.DAY26==0) ? "" : Number(rec.DAY26).toString();	vo.sum += Number(rec.DAY26);
				vo.DAY27 = (rec.DAY27==0) ? "" : Number(rec.DAY27).toString();	vo.sum += Number(rec.DAY27);
				vo.DAY28 = (rec.DAY28==0) ? "" : Number(rec.DAY28).toString();	vo.sum += Number(rec.DAY28);
				vo.DAY29 = (rec.DAY29==0) ? "" : Number(rec.DAY29).toString();	vo.sum += Number(rec.DAY29);
				vo.DAY30 = (rec.DAY30==0) ? "" : Number(rec.DAY30).toString();	vo.sum += Number(rec.DAY30);
				vo.DAY31 = (rec.DAY31==0) ? "" : Number(rec.DAY31).toString();	vo.sum += Number(rec.DAY31);
				list.addItem(vo);
			}
			return list;
		}	
	}
}